package com.jjwxc.jwjskandriod.widget.dialog

import android.text.TextUtils
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.jjwxc.jwjskandriod.R
import com.jjwxc.jwjskandriod.framework.utils.FFUtils

/**
 * 自定义弹窗
 */
class MyDialogFragment(private val content: String?,
                       private val leftText: String?,
                       private val rightText: String?)
    : BaseBottomDialogFragment() {
    private lateinit var llBottom: LinearLayout
    private lateinit var view: View
    private lateinit var btnRight: TextView
    private lateinit var btnLeft: TextView
    private lateinit var tvContent: TextView
    lateinit var rightCallback: () -> Unit
    lateinit var leftCallback: () -> Unit

    override fun setContentViewID(): Int = R.layout.fragment_my_dialog

    override fun findViewIDToOnClick() {
        tvContent = baseDialog.findViewById(R.id.tvContent)
        llBottom = baseDialog.findViewById(R.id.ll_Bottom)
        view = baseDialog.findViewById(R.id.view)
        btnRight = baseDialog.findViewById(R.id.btn_confirm)
        btnLeft = baseDialog.findViewById(R.id.btn_cancel)

        initView()
    }

    private fun initView() {
        btnRight.setOnClickListener {
//            dismissDialog()   // 该弹窗不允许取消
            if (::rightCallback.isInitialized) {
                rightCallback.invoke()
            }
        }

        btnLeft.setOnClickListener {
//            dismissDialog()
            if (::leftCallback.isInitialized) {
                leftCallback.invoke()
            }
        }

        // 左按钮
        if (TextUtils.isEmpty(leftText)) {
            btnLeft.visibility = View.GONE
            view.visibility = View.GONE

            val layoutParams = llBottom.layoutParams
            layoutParams.width = FFUtils.dp2px(138F);

            llBottom.layoutParams = layoutParams
        } else {
            btnLeft.text = leftText
        }
        
        // 右按钮
        if (!TextUtils.isEmpty(rightText)) {
            btnRight.text = rightText
        }

        // 文字内容
        if (!TextUtils.isEmpty(content)) {
            tvContent.text = content
        } else {
            tvContent.visibility = View.GONE
        }
    }
}